<?php

namespace app\wx\controller;

use think\Db;
use think\facade\Request;
use think\Validate;

class Msg extends Base
{

    protected $beforeActionList = [
        'first',
    ];
    protected $uid = '';

    protected function first()
    {
        if (session('uid') || input('openid')) {
            if (session('uid')) {
                $this->uid = session('uid');
            } else {
                $this->uid = DB::name('admin')->where('openid', input('openid'))->value('id');
                if($this->uid){
                    session('uid', $this->uid);
                }else{
                    $url=url('/wx/interest/index' , '', '', Request::host());
                    $this->redirect($url);
                }
            }
        } else {
            $url = url('project/modelindex?model_id=' . input('model_id'), '', '', Request::host());
            $this->redirect('https://openwx.weibuyun.com/wxuser/get_openid' . '?domain=' . Request::host() . '&url=' . $url);
        }
    }
    public function index()
    {
        return view();
    }

    public function reg()
    {

        if ($this->request->isPost()) {
            if (session('openid') == '') {
                $this->error('请先关注公众号后在在微信中打开本页面');
            }

            $post['title'] = input('post.title');
            $post['info'] = input('post.info');
            $post['add_time'] = time();
            $post['openid'] = session('openid');
            $post['status'] = 2;

            if(!isset($post['title']) || empty($post['title'])){
                $this->error('标题没有输入');
            }
            if(!isset($post['info']) || empty($post['info'])){
                $this->error('详细内容没有输入');
            }
            if (Db::name('diamond')->insert($post)) {
                $this->success('您成功发布表扬信息');
            } else {
                $this->error('您发布表扬信息失败,请稍后再试!');
            }

        } else {
                return view();
        }

    }




    public function info()
    {
        $info = Db::name('diamond')->where('id', input('get.id'))->find();
       // dump($info);
        $this->assign('info', $info);

        return view();

    }

    public function lists()
    {
        $map = array();
        $data = array();
        $post = $this->request->get();

        unset($post['page']);
        unset($post['limit']);
        unset($post['status']);
        $post['status']=1;
        //dump($map);
        $data['count'] = Db::name('diamond')
            ->where($post)
            ->where($map)
            ->count();
        // echo Db::name('supplier')->where($map)->fetchSql(true)->select();
        $data['data'] = Db::name('diamond')
            ->where($post)
            ->where($map)
            ->order(['add_time desc', 'id desc'])
            ->page(input("get.page"), 5)->select();

        if ($data['count'] > 0 && $data['data']) {
            $data['code'] = 0;
            $data['msg'] = '获取成功';
        } else {
            $data['code'] = 1;
            $data['msg'] = '没有更多信息';
        }

        return json($data);
    }


}
